package com.exam.controller;

import com.exam.service.ExaminationService;
import com.exam.utils.ResultVOUtil;
import com.exam.utils.paramAccpet.ExaminationInfo;
import com.exam.vo.ResultVO;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.LinkedHashMap;
import java.util.Map;

/**
 * Created by v
 * Date:2019/1/16
 * Time:15:36
 */
@RestController
@RequestMapping("/admin/examination")
@Slf4j
public class ExaminationController {

    @Autowired
    private ExaminationService examinationService;

    /**
     * 试卷发布
     * @param examinationInfo
     * @return
     */
    @PostMapping("/launch")
    public ResultVO examinationLaunch(@Valid @RequestBody ExaminationInfo examinationInfo) {

        examinationService.designExamination(examinationInfo);

        return ResultVOUtil.success("发布成功");

    }

    @GetMapping("/list")
    public ResultVO examinationList(@RequestParam(value = "page", defaultValue = "1") Integer page,
                                    @RequestParam(value = "size", defaultValue = "10") Integer size) {

//        Integer userId = RedisUserIdUtil.getAdminUserId(request,redisTemplate);
        PageInfo<Map<String, Object>> all = examinationService.getExaminationList(page, size);

        Map<String, Object> map = new LinkedHashMap<>();
        map.put("number", all.getTotal());
        map.put("currentPage", page);
        map.put("data", all.getList());
        return ResultVO.success(map);
    }

    /**
     * 试卷详情
     * @param examinationId
     * @return
     */
    @GetMapping("/detail")
    public ResultVO examinationDetail(@RequestParam(value = "examinationId") Integer examinationId) {

        return ResultVOUtil.success(examinationService.getExamination(examinationId));
    }
}
